//
// Created by djy on 2019/7/23.
//

#ifndef CLION_CODE_SETTING_H
#define CLION_CODE_SETTING_H

#include <stdint.h>
//#include "board.h"
#include "virtual_flash.h"

#define FLASH_SECTOR_SIZE       (1024u)
#define FLASH_SECTOR_MASK       (FLASH_SECTOR_SIZE - 1)

#define SETTING_MAGIC           (0x55AAu)
#define SETTING_FROM            FLASH_START_ADDR
#define SETTING_SIZE            FLASH_SIZE
#define SETTING_INVALID_ADDR    (SETTING_FROM + SETTING_SIZE)



typedef struct setting_hdr
{
    uint16_t  magic;
    uint16_t  crc;
} setting_hdr_t;

typedef struct
{
    uint8_t sn[12];
    uint8_t dkey[8];
    uint8_t id[4];
    uint8_t pwd[2];
} encode_t;

typedef struct
{
    uint8_t gid[4];
    uint8_t panid[2];
    uint8_t sid[2];
} para_t;

typedef struct CtrlInfo
{
    uint8_t idx;
    uint8_t aid[4];
    uint8_t chn;
} ctrl_info_t;

#define AID_LEN 4
typedef struct DevInfo
{
    uint8_t report;
    uint8_t body_report_freq[2];
    uint8_t body_report_step;
    uint8_t lux_report_freq[2];
    uint8_t lux_report_step[2];
    uint8_t temp_report_freq[2];
    uint8_t temp_report_step[2];
    uint8_t humi_report_freq[2];
    uint8_t humi_report_step[2];
    uint8_t body_sensitive;
    uint8_t nobody_duration[2];
    uint8_t power_on_delay_time[2];
    uint8_t compensate_percent;
    uint8_t sensor_archives[8][4];//32B
    uint8_t sensor_chn_nums;
    uint8_t dev_mode;
    uint8_t extra_data[2];
}dev_info_t;


typedef struct sensor
{
    uint16_t cur_lux;
    uint8_t	 hal_body_status;
    uint8_t	 soft_body_status;
    uint8_t	 body_status;
    uint16_t pir_ad;
} sensor_status_t;

typedef struct
{
    setting_hdr_t hdr;      //u16*2     4B
    /* device */
    encode_t encode;        //u8        26B

    /* para */
    para_t para;            //u8        8B

    /* ctrl */
    ctrl_info_t ctrl[2];    //u8        12B (6B*2)

    //-----------------------------------50B-----------------
    /* dev */
    dev_info_t dev_infor;   //u8        54B
    //-----------------------------------104B-----------------
}__attribute__((aligned(4)))setting_t;

extern setting_t setting;

#define SETTING_HDR_SIZE    (sizeof(setting_hdr_t))
#define SETTING_DAT_SIZE    (sizeof(setting)-sizeof(setting_hdr_t))
#define setting_data_addr(s)     ((uint8_t*)&s + SETTING_HDR_SIZE)


void setting_save(void);
void setting_load(void);

void dev_show(void);
#endif //CLION_CODE_SETTING_H
